package Cursus.Listeners;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

/**
 *
 * @author sonicah
 */
public class myTextField extends JPanel {
    boolean focus = false;
    Timer tim;
    char[] letter = new char[100];
    int aantal=0,offset;

    public myTextField() {
        setPreferredSize(new Dimension(100,30));
        grabFocus();
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                if(aantal < 100) {
                    letter[aantal] = e.getKeyChar();
                    aantal++;
                }
                repaint();
            }
        });

        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                focus = true;
                repaint();
                grabFocus();
            }
        });

        addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                focus = false;
                repaint();
            }
        });

        tim = new Timer(200, new ActionListener() { 
            public void actionPerformed(ActionEvent e) {
                repaint();
            }
        });
        tim.start();
    }

    public void paint(Graphics g) {
        super.paint(g);
        if(focus) {
            g.setColor(Color.yellow);
        } else {
            g.setColor(Color.white);
        }
        g.fillRect(1, 1, 100, 30);
        g.setColor(Color.black);
        g.drawRect(1, 1, 100, 30);
        for(int i=0; i<aantal; i++) {
            offset = (int)(Math.random()*10);
            g.drawString(""+letter[i], i*8, 10+offset);
        }
    }
}
